﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.App_Code
{
    using Autofac;
    using Autofac.Integration.Mvc;
    using ECommon.Autofac;
    using ECommon.Components;
    using ECommon.Configurations;
    using ECommon.JsonNet;
    using ECommon.Log4Net;
    using ECommon.Logging;
    using ENode.Configurations;
    using Infrastructure;
    using System.Reflection;
    using System.Web.Mvc;

    public class Bootstrap
    {
        private ILogger _logger;
        private Configuration _ecommonConfiguration;
        private ENodeConfiguration _enodeConfiguration;

        public void Initialize()
        {
            ConfigSettings.Initialize();

            InitializeECommon();
            try
            {
                InitializeENode();
            }
            catch (Exception ex)
            {
                _logger.Error("Initialize ENode failed.", ex);
                throw;
            }
        }
        public void Start()
        {
            try
            {
                ENodeExtensions.Start(_enodeConfiguration);
            }
            catch (Exception ex)
            {
                _logger.Error("EQueue start failed.", ex);
                throw;
            }


            RegisterControllers();
        }

        private void InitializeECommon()
        {
            _ecommonConfiguration = Configuration
                .Create()
                .UseAutofac()
                .RegisterCommonComponents()
                .UseLog4Net()
                .UseJsonNet()
                .RegisterUnhandledExceptionHandler();

            _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);
            _logger.Info("ECommon initialized.");
        }

        private void InitializeENode()
        {

            var assemblies = new[]
            {
                Assembly.Load("QueryServices"),
                Assembly.Load("QueryServices.Ef"),
                Assembly.GetExecutingAssembly()
            };

            _enodeConfiguration = _ecommonConfiguration
                .CreateENode()
                .RegisterENodeComponents()
                .RegisterBusinessComponents(assemblies);

            _enodeConfiguration = ENodeExtensions.RegisterAllTypeCodes(_enodeConfiguration);
            _enodeConfiguration = ENodeExtensions.UseEQueue(_enodeConfiguration);
            _enodeConfiguration.InitializeBusinessAssemblies(assemblies);
            ENodeExtensions.Start(_enodeConfiguration);
            _logger.Info("ENode initialized.");


        }
        private void RegisterControllers()
        {
            var webAssembly = Assembly.GetExecutingAssembly();
            var container = (ObjectContainer.Current as AutofacObjectContainer).Container;
            var builder = new ContainerBuilder();
            builder.RegisterControllers(webAssembly);
            builder.Update(container);
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}